﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using HM.UI.HMService;
using GalaSoft.MvvmLight.Command;

namespace HM.UI.ViewModels.Management
{
    public class ManageExpensesViewModel : INotifyPropertyChanged
    {
        private HMServiceClient proxy = new HMServiceClient();

        public ManageExpensesViewModel()
        {
            this.RefreshExpenses();
            SaveChanges = new RelayCommand(SaveExpensesExecute);
            RemoveSelected = new RelayCommand(RemoveSelectedExecute, CanExecuteRemoveSelected);
        }

        #region Refresh

        private void RefreshExpenses()
        {
            this.proxy.GetExpensesCompleted += (s, e) =>
            {
                this.Expenses = e.Result;
            };
            this.proxy.GetExpensesAsync();
        }

        #endregion

        #region Execute

        public RelayCommand SaveChanges { get; private set; }

        private void SaveExpensesExecute()
        {
            if (this.Expenses != null)
            {
                this.proxy.SaveExpensesCompleted += (s, e) =>
                {
                    this.RefreshExpenses();
                };
                this.proxy.SaveExpensesAsync(this.Expenses.ToArray());
            }
        }

        public RelayCommand RemoveSelected { get; private set; }

        private void RemoveSelectedExecute()
        {
            this.proxy.RemoveExpenseCompleted += (s, e) =>
            {
                this.RefreshExpenses();
            };
            this.proxy.RemoveExpenseAsync(this.SelectedExpense);
        }

        #endregion

        #region CanExecute

        private bool CanExecuteRemoveSelected()
        {
            return this.SelectedExpense != null;
        }

        #endregion

        #region Properties

        private IEnumerable<Expense> expenses;

        public IEnumerable<Expense> Expenses
        {
            get
            {
                return this.expenses;
            }

            set
            {
                if (expenses != value)
                {
                    this.expenses = value;
                    this.OnPropertyChanged("Expenses");
                }
            }
        }

        private Expense selectedExpense;

        public Expense SelectedExpense
        {
            get
            {
                return this.selectedExpense;
            }

            set
            {
                if (selectedExpense != value)
                {
                    this.selectedExpense = value;
                    this.OnPropertyChanged("selectedExpense");
                }
            }
        }

        #endregion

        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

    }
}
